<template>
	<view>
		<!-- <uni-sign-in ref="signIn"></uni-sign-in> -->
		<view style="width: 100%;height: 350rpx;background-image: url('static/9.png') ;background-size: 100% 100%;: ;">
			<view style="display: flex; flex-direction: column;">
				<view style="display: flex;justify-content: center;" @click="update_touxiang">
					<image
						style="display: flex;height: 150rpx;width: 150rpx;border-radius: 50%;border: white solid 3rpx; margin-top: 50rpx;"
						:src="src" mode="aspectFill"></image>
				</view>
				<view
					style="display: flex;justify-content: center;margin-top: 10rpx;font-size: 40rpx;font-weight: 500;color: white;">
					{{quankezx_name_tel.name}}
				</view>
			</view>
		</view>
		<view v-for="item in caidanlists"
			v-if="quankezx_userinfo.permission.indexOf(item.quanxian)!=-1 || item.quanxian=='all'"
			@click="tiaozhan(item.url)"
			style="display: flex;width: 85%;margin:20rpx auto 0 auto;box-shadow: 1px 1px 5px #c2c2c2;background-color: white;border-radius: 20rpx; flex-direction: row; justify-content: space-between;padding: 20rpx 30rpx;">
			<view class="">
				{{item.name}}
			</view>
			<uni-icons type="forward"></uni-icons>
		</view>

		<!-- 弹窗蒙层 -->
		<view class="mask" v-if="popupVisible" @click="hidePopup">
			<!-- 弹窗内容 -->
			<view class="popup">
				<view class="popup-header">
					<text class="title">型号折扣</text>
					<text class="close" @click="hidePopup">×</text>
				</view>
				<view style="padding: 20rpx;">
					<view
						style="display: flex;margin:0 0 25rpx 0;border-bottom:gainsboro 3rpx solid ;padding-bottom: 15rpx;align-items: center;">
						<view style="display: flex;flex:1;">
							名称
						</view>
						<view style="display: flex;flex:1;">
							型号
						</view>
						<view style="display: flex;flex:1;">
							折扣
						</view>
						<view style="font-size: 25rpx;flex: 2;">
							备注
						</view>
					</view>

					<view v-for="item in myzhekoulist" style="display: flex; margin: 20rpx 0;align-items: center;">
						<view style="display: flex;flex:1;">
							{{item.xinghaoInfo[0].name}}
						</view>
						<view style="display: flex;flex:1;">
							{{item.xinghaoInfo[0].xinghao}}
						</view>
						<view style="display: flex;flex:1;">
							{{item.zhekou}}
						</view>
						<view style="font-size: 25rpx;flex: 2;">
							{{item.xinghaoInfo[0].beizhu}}
						</view>
					</view>

				</view>
				<view class="popup-footer">
					<button @click="hidePopup">确认</button>
				</view>
			</view>
		</view>



		<view @click="tuichu"
			style="display: flex;width: 40%;background-color: blue;color: white;border-radius: 20rpx;align-items: center;justify-content: center;padding: 20rpx;margin: 40rpx auto 20rpx auto;">
			退出登陆
		</view>
	</view>
</template>

<script>
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				popupVisible: false,
				myzhekoulist: [],
				caidanlists: [{
						name: "我的资料",
						quanxian: "all",
						url: "/uni_modules/uni-id-pages/pages/userinfo/userinfo"
					},
					{
						name: "修改轮播图",
						// quanxian: "xiugai_lunbotu",
						quanxian: "all",
						url: "/pages/banner/banner"
					},
					{
						name: "礼品卡型号",
						// quanxian: "xiugai_lunbotu",
						quanxian: "all",
						url: "/pages/lipinka_xinghao/list"
					},
					{
						name: "我的折扣",
						quanxian: "all",
						url: "zhekou"
					},
					{
						name: "添加商品分类",
						quanxian: "all",
						// quanxian: "add_shangpinfenlei",
						url: "/pages/shangpinfenlei_add/shangpinfenlei_add"
					},
					{
						name: "添加商品",
						quanxian: "all",
						// quanxian: "add_shangpin",
						url: "/pages/shangpin_add/shangpin_add"
					},
					{
						name: "设置",
						quanxian: "all",
						url: ""
					},
					{
						name: "技术支持",
						quanxian: "all",
						url: ""
					},
					{
						name: "重置密码",
						quanxian: "all",
						url: "/uni_modules/uni-id-pages/pages/retrieve/retrieve"
					},
				],
				src: '',
				quankezx_userinfo: {
					permission: []
				},
				quankezx_name_tel: []
			}
		},
		computed: {
			hasLogin() {
				return store.hasLogin
			}
		},
		async onLoad() {
			this.quankezx_name_tel = await uni.getStorageSync('quankezx_name_tel')
			this.quankezx_userinfo = await uniCloud.getCurrentUserInfo()
			console.log("quankezx_name_tel", this.quankezx_name_tel);
			console.log("quankezx_userinfo", this.quankezx_userinfo);
			this.src = this.quankezx_name_tel.avatar ? this.quankezx_name_tel.avatar :
				'../../static/uni-center/defaultAvatarUrl.png'

		},
		methods: {
			showPopup() {
				this.popupVisible = true;
			},
			hidePopup() {
				this.popupVisible = false;
			},
			// 显示折扣弹窗
			showDiscountPopup() {
				uni.showLoading({
					title: '数据加载中'
				})
				this.myzhekoulist = [];
				this.showPopup()
				// 获取当前经销商ID（示例，实际项目中需要根据业务逻辑获取）
				if (!this.quankezx_name_tel._id) {
					uni.showToast({
						title: '未获取到经销商信息',
						icon: 'none'
					});
					this.loading = false;
					return;
				}
				// 调用云函数获取折扣数据
				uniCloud.callFunction({
					name: 'getDiscountList',
					data: {
						jingxiaoshang_id: this.quankezx_name_tel._id
					}
				}).then(res => {
					uni.hideLoading()
					this.loading = false;
					if (res.result && res.result.data) {
						this.myzhekoulist = res.result.data;
					} else {
						uni.hideLoading()
						uni.showToast({
							title: '获取折扣数据失败',
							icon: 'none'
						});
					}
				})
			},

			//跳转
			tiaozhan(e) {
				if(e=="zhekou"){
					this.showDiscountPopup()
						return
				}else{
					uni.navigateTo({
						url: e
					});
				}
			},
			update_touxiang() {
				uni.navigateTo({
					url: "/uni_modules/uni-id-pages/pages/userinfo/userinfo"
				})
			},
			async tuichu() {
				await mutations.logout()
				// uniIdCo.logout()
				// uni.removeStorageSync('quankezx_userinfo')
				// uni.navigateTo({
				// 		url: "/pages/denglu/denglu"
				// })
			},
			toSettings() {
				uni.navigateTo({
					url: "/pages/ucenter/settings/settings"
				})
			},
			ucenterListClick(item) {
				if (!item.to && item.event) {
					this[item.event]();
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f8f8f8;
	}


	.popup {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 740rpx;
		background-color: white;
		border-radius: 16rpx;
		z-index: 1001;
		overflow: hidden;
	}

	.popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 24rpx 32rpx;
		background-color: #f8f8f8;
		border-bottom: 1rpx solid #eee;
	}

	.title {
		display: flex;
		width: 749rpx;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}



	.loading,
	.empty {
		text-align: center;
		padding: 40rpx 0;
		font-size: 28rpx;
		color: #999;
	}

	.discount-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.discount {
		font-size: 28rpx;
		color: #e64340;
	}

	.container {
		padding: 30rpx;
	}

	.mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}



	.close {
		font-size: 32rpx;
		color: #999;
	}

	.popup-footer {
		padding: 24rpx 32rpx;
		border-top: 1rpx solid #eee;
		text-align: center;
	}
</style>